<?php
	class memberControl extends commonControl{
		/**
		 * 显示所有会员
		 */
		public function members(){
			$db = K('userQianTairelation');
			$row = $db->all();
			$this->assign('users', $row);
			$this->display();
		}
		/**
		 * 添加用户
		 */
		public function add_member_show(){
			$role = M('role');
			$roles = $role->where(" status = 1")->where(" isadmin = 0")->all();
			$this->assign('roles', $roles);
			$this->display();
		}
		/**
		 * 执行会员添加
		 */
		public function add_member_exe(){
			$_POST['regtime'] = time();
			$db = R("user");
			$db->join = array(
					"user_role"=>array(
							"type"=>"has_many",
							'foreign_key'=>'uid',
						),
				);
			
			if($db->add()){
				$this->success("会员添加成功");
			}else{
				$this->error("可惜，会员添加失败");
			}
		}
		/**
		 * 执行会员删除
		 */
		public function member_del(){
			P($_GET);
			$uid = isset($_GET['uid']) && !empty($_GET['uid'])?$_GET['uid']:false;
			if($uid === false){
				$this->error('参数错误，需要会员ID');
			}
			$user = R('user');
			$user->validate = array(
					array('username','nonull','会员名不能为空',2),
					array('username','user','用户名格式不正确',2),
					array('password','regexp:/^[a-zA-Z0-9]{6,20}$/','密码长度或格式不对',2),
					array('email','email','邮箱格式不正确',2),
				);
			if($user->validate()){ //验证通过开始插入数据
				$user->join = array(
						'user_role'=>array(
								'type'=>'has_many',
								'foreign_key'=>'uid',
							),
					);
				if($user->where(" uid in (".$uid.")")->del()){
					$this->success("恭喜删除成功");
				}else{
					$this->error('删除失败');
				}
			}else{
				$this->error($user->error);
			}
		}
		/**
		 * 添加会员组
		 */
		public function add_mem_type(){
			$this->display();
		}
		/**
		 * 执行增加会员组
		 */
		public function add_memtype_exe(){
			$role = M('role');
			$role->validate = array(
					array('rname','nonull','会员组名称不能为空',2),
					array('rname',"regexp:/^[a-zA-Z][a-zA-Z0-9]{4,20}$/",'用户名不能小于5为大于21位',2),
				);
			if($role->validate()){
				if($role->add()){
					$this->success('添加会员组成功','show_mem_type');
				}else{
					$this->error('添加失败！');
				}
			}else{
				$this->error($role->error);
			}
			
		}
		/**
		 * 显示所有会员组
		 */
		public function show_mem_type(){
			$role=  M('role');
			$roles = $role->where("isadmin = 0")->all();
			$this->assign('roles', $roles);
			$this->display();
		}
		/**
		 * 会员组级别
		 */
		public function mem_type_level(){
			$this->display();
		}
	}

?>